package portlet;


import java.util.Map;

import javax.faces.context.FacesContext;

import javax.naming.NamingException;

import oracle.stellent.ridc.IdcClient;
import oracle.stellent.ridc.IdcClientException;
import oracle.stellent.ridc.IdcContext;

import oracle.webcenter.content.stellent.StellentUtils;
import oracle.webcenter.doclib.internal.config.DocLibADFConfigUtils;

import org.apache.myfaces.trinidad.context.RequestContext;

public class Global {
    private static IdcClient idcClient;
    private static IdcContext userContext;
    private static String connectionName;
    private static String webroot;
    public static String username = "weblogic";

    public Global() {
        super();
    }

    public static void setIdcClient(IdcClient idcClient) {
        Global.idcClient = idcClient;
    }

    public static IdcClient getIdcClient() {

        if (idcClient == null)
            try {
                idcClient = StellentUtils.getIdcClient(getConnectionName());
            } catch (NamingException e) {
                e.printStackTrace();
            } catch (IdcClientException e) {
                e.printStackTrace();
            }


        return idcClient;
    }

    public static void setUserContext(IdcContext userContext) {
        Global.userContext = userContext;
    }

    public static IdcContext getUserContext() {
        if (userContext == null)
            userContext = new IdcContext(username);
        return userContext;
    }

    public static void setConnectionName(String connectionName) {
        Global.connectionName = connectionName;
    }

    public static String getConnectionName() {
        if (connectionName == null)
            connectionName = DocLibADFConfigUtils.getPrimaryConnectionName();
        return connectionName;
    }

    public static void setWebroot(String webroot) {
        Global.webroot = webroot;
    }

    public static String getWebroot() {
        if (webroot == null)
            webroot =
                    FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
        return webroot;
    }
    public static void setPageFlowScope(String key, Object value) {
        Map pageFlowScope =
            RequestContext.getCurrentInstance().getPageFlowScope();
        Object myObject = pageFlowScope.put(key, value);
    }

    public static Object getPageFlowScope(String key) {
        Map pageFlowScope =
            RequestContext.getCurrentInstance().getPageFlowScope();
        Object myObject = pageFlowScope.get(key);
        return myObject;
    }
}

